const fs = require('fs')
const { queryAvatarWithId } = require("../service/file.service");
const userService = require("../service/user.service");
const { UPLOAD_PATH } = require('../config/path');

class UserController {
    async create(ctx, next) {
        // 1. 获取用户传递过来数据
        const user = ctx.request.body;
        // 2. 将user信息存储到数据库中
        const result = await userService.create(user)
        // 3.查看存储结果，告知前端创建成功 
        ctx.body = {
            message: '创建用户成功～',
            data: result
        }
    }
    async showAvatarImage(ctx, next) {
        // 1. 获取userId
        const { userId } = ctx.params;
        // 2. 根据 userId 查看对应头像
        const avatarInfo = await queryAvatarWithId(userId);
        // 3. 获取头像所在的文件
        const { filename, mimetype } = avatarInfo
        ctx.type = mimetype

        ctx.body = fs.createReadStream(`${UPLOAD_PATH}/${filename}`);

    }
}

module.exports = new UserController()